package yamen.bdwm.board;

import yamen.bdwm.BoardFragment;
import yamen.bdwm.MainActivity;
import yamen.bdwm.R;
import yamen.bdwm.compatible.ActionBarActivity;
import yamen.bdwm.data.WmMyData;
import yamen.bdwm.data.interfaces.onListRefreshedListener;
import yamen.bdwm.datastruct.WmDataAdapter;
import yamen.bdwm.datastruct.WmItem;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class TopTenListFragment extends ListFragment implements
		onListRefreshedListener {
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.list, container, false);
		WmMyData.getinstance().toptenlist.CreateAdapter(0, R.layout.thread_item);
		final WmDataAdapter adapter = WmMyData.getinstance().toptenlist.getAdapter(0,this.getActivity());

		((ListView) v.findViewById(R.id.listView1)).setAdapter(adapter);
		
		((ListView) v.findViewById(R.id.listView1))
				.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Fragment f = ((MainActivity) TopTenListFragment.this
								.getActivity()).getCurrentFragment();
						if (f instanceof BoardFragment) {
							
							((BoardFragment) f).showThread(((WmItem)adapter.getItem(position)).url,((WmItem)adapter.getItem(position)).title);
						}
					}

				});
		if (adapter.getCount() == 0) {
			((ActionBarActivity)this.getActivity()).SetRefreshingDialog(true);
			WmMyData.getinstance().toptenlist.getRefreshed(this);
		}
		return v;

	}

	public void onRefreshed(int numberOfRefreshed) {
		
		
		if (getActivity() instanceof ActionBarActivity) {
			((ActionBarActivity)this.getActivity()).SetRefreshingDialog(false);
			((ActionBarActivity) getActivity()).getActionBarHelper()
					.setRefreshActionItemState(false);
		}
	}
	public void onError(int i) {
		((ActionBarActivity)this.getActivity()).SetRefreshingDialog(false);
		if (getActivity() instanceof ActionBarActivity) {
			((ActionBarActivity) getActivity()).getActionBarHelper()
					.setRefreshActionItemState(false);
		}
		
	}

}
